<?php
/**
 * 项目管理模块
 * User: lyc
 * Date: 2016/11/18
 * Time: 10:56
 */
class ItemService extends BaseService
{
    private static $self           =   NULL;
    private $mItem                 =   NULL;
    private $mEnterprise           =   NULL;
    private $mRiskControl          =   NULL;
    private $mUserAward            =   NULL;
    private $mInvest               =   NULL;
    private $mRefundItemPlan       =   NULL;
    private $mUser       =   NULL;
    private $mUserTicket       =   NULL;
    private $mExperItem       =   NULL;

    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }
        return self::$self;
    }


    public function __construct()
    {
        $this->mItem                   =    new Involved_ItemModel();
        $this->mEnterprise             =    new Involved_EnterpriseModel();
        $this->mRiskControl            =    new Involved_RiskControlModel();
        $this->mInvest                 =    new Involved_InvestModel();
        $this->mUserAward              =    new Involved_UserAwardModel();
        $this->mRefundItemPlan         =    new Involved_RefundItemPlanModel();
        $this->mUser                   =    new Involved_UserModel();
        $this->mUserTicket             =    new Involved_UserTicketModel();
        $this->mExperItem              =    new Involved_ExperItemModel();

    }


    /**
     * 企业列表
     */
    public function CompanyList(){
        return $this->mEnterprise->CompanyList();

    }
    /**
     * 企业添加
     * @param $paramObj
     * @return int
     */
    public function CompanyAdd($paramObj)
    {
        $paramObj->logo = !empty($paramObj->logo) ?
            $paramObj->logo =  QiniuUpload::instance()->upload_files($paramObj->logo):'';
        $paramObj->business_license = !empty($paramObj->business_license) ?
            $paramObj->business_license =  QiniuUpload::instance()->upload_files($paramObj->business_license):'';
        $paramObj->tax_license = !empty($paramObj->tax_license) ?
            $paramObj->tax_license =  QiniuUpload::instance()->upload_files($paramObj->tax_license):'';
        $paramObj->place_license = !empty($paramObj->place_license) ?
            $paramObj->place_license =  QiniuUpload::instance()->upload_files($paramObj->place_license):'';
        $paramObj->org_license = !empty($paramObj->org_license) ?
            $paramObj->org_license =  QiniuUpload::instance()->upload_files($paramObj->org_license):'';
        $paramObj->account_tax_license = !empty($paramObj->account_tax_license) ?
            $paramObj->account_tax_license =  QiniuUpload::instance()->upload_files($paramObj->account_tax_license):'';
        $paramObj->identity_card_z = !empty($paramObj->identity_card_z) ?
            $paramObj->identity_card_z =  QiniuUpload::instance()->upload_files($paramObj->identity_card_z):'';
        $paramObj->identity_card_f = !empty($paramObj->identity_card_f) ?
            $paramObj->identity_card_f =  QiniuUpload::instance()->upload_files($paramObj->identity_card_f):'';
        $paramObj->sanzheng = !empty($paramObj->sanzheng) ?
            $paramObj->sanzheng =  QiniuUpload::instance()->upload_files($paramObj->sanzheng):'';
        $aData = [
            'name'        => $paramObj->name,
            'registered_address'      => $paramObj->registered_address,
            'registered_capital'              => $paramObj->registered_capital,
            'registered_time'               => $paramObj->registered_time,
            'company_size'       => $paramObj->company_size,
            'company_status'           => $paramObj->company_status,
            'corporation_name'              => $paramObj->corporation_name,
            'phone'               => $paramObj->phone,
            'whether_crime'                => $paramObj->whether_crime,
            'company_account'                => $paramObj->company_account,
            'bankcode'                => $paramObj->bankcode,
            'brief'                => $paramObj->brief,
            'logo'                => $paramObj->logo,
            'business_license'                => $paramObj->business_license,
            'tax_license'                => $paramObj->tax_license,
            'place_license'                => $paramObj->place_license,
            'org_license'                => $paramObj->org_license,
            'account_tax_license'                => $paramObj->account_tax_license,
            'identity_card_z'                => $paramObj->identity_card_z,
            'identity_card_f'                => $paramObj->identity_card_f,
            'sanzheng'                => $paramObj->sanzheng,
            'audit_status' =>     $paramObj->sanzheng,
            'businessLicense' =>     $paramObj->businessLicense,
            'taxNo' =>    $paramObj->taxNo,
            'unifiedCode' =>     $paramObj->unifiedCode,
            'orgNo' =>     $paramObj->orgNo,
            'creditCode' =>     $paramObj->creditCode,
            'contact' =>    $paramObj->contact,
            'contactPhone' =>     $paramObj->contactPhone,
            'add_time'             => time()
        ];

        return $this->mEnterprise->insert($aData);

    }
    /**
     * 公司修改
     * @param $paramObj
     * @return bool
     */
    public function CompanyUp($paramObj)
    {

        #图片上传
        $paramObj->logo = !empty($paramObj->logo) ?
            $paramObj->logo =  QiniuUpload::instance()->upload_files($paramObj->logo):$paramObj->logo_old;
        $paramObj->business_license = !empty($paramObj->business_license) ?
            $paramObj->business_license =  QiniuUpload::instance()->upload_files($paramObj->business_license):$paramObj->business_license_old;
        $paramObj->tax_license = !empty($paramObj->tax_license) ?
            $paramObj->tax_license =  QiniuUpload::instance()->upload_files($paramObj->tax_license):$paramObj->tax_license_old;
        $paramObj->place_license = !empty($paramObj->place_license) ?
            $paramObj->place_license =  QiniuUpload::instance()->upload_files($paramObj->place_license):$paramObj->place_license_old;
        $paramObj->org_license = !empty($paramObj->org_license) ?
            $paramObj->org_license =  QiniuUpload::instance()->upload_files($paramObj->org_license):$paramObj->org_license_old;
        $paramObj->account_tax_license = !empty($paramObj->account_tax_license) ?
            $paramObj->account_tax_license =  QiniuUpload::instance()->upload_files($paramObj->account_tax_license):$paramObj->account_tax_license_old;
        $paramObj->identity_card_z = !empty($paramObj->identity_card_z) ?
            $paramObj->identity_card_z =  QiniuUpload::instance()->upload_files($paramObj->identity_card_z):$paramObj->identity_card_z_old;
        $paramObj->identity_card_f = !empty($paramObj->identity_card_f) ?
            $paramObj->identity_card_f =  QiniuUpload::instance()->upload_files($paramObj->identity_card_f):$paramObj->identity_card_f_old;
        $paramObj->sanzheng = !empty($paramObj->sanzheng) ?
            $paramObj->sanzheng =  QiniuUpload::instance()->upload_files($paramObj->sanzheng):$paramObj->sanzheng_old;

        $aData = [
            'name'        => $paramObj->name,
            'registered_address'      => $paramObj->registered_address,
            'registered_capital'              => $paramObj->registered_capital,
            'registered_time'               => $paramObj->registered_time,
            'company_size'       => $paramObj->company_size,
            'company_status'           => $paramObj->company_status,
            'corporation_name'              => $paramObj->corporation_name,
            'phone'               => $paramObj->phone,
            'whether_crime'                => $paramObj->whether_crime,
            'company_account'                => $paramObj->company_account,
            'bankcode'                => $paramObj->bankcode,
            'brief'                => $paramObj->brief,
            'logo'                => $paramObj->logo,
            'business_license'                => $paramObj->business_license,
            'tax_license'                => $paramObj->tax_license,
            'place_license'                => $paramObj->place_license,
            'org_license'                => $paramObj->org_license,
            'identity_card_z'                => $paramObj->identity_card_z,
            'identity_card_f'                => $paramObj->identity_card_f,
            'sanzheng'                => $paramObj->sanzheng,
            'account_tax_license'                => $paramObj->account_tax_license,
            'businessLicense' =>     $paramObj->businessLicense,
            'taxNo' =>    $paramObj->taxNo,
            'unifiedCode' =>     $paramObj->unifiedCode,
            'orgNo' =>     $paramObj->orgNo,
            'creditCode' =>     $paramObj->creditCode,
            'contact' =>    $paramObj->contact,
            'contactPhone' =>     $paramObj->contactPhone,

        ];
        return $this->mEnterprise->update($aData,['auto_id'=>$paramObj->auto_id]);
    }
    /**
     * @param $auto_id
     * @return mixed
     * 根据企业ID查询信息
     */
    public function GetEnterprise($auto_id){
        return $this->mEnterprise->GetEnterprise($auto_id);

    }
    /**
     * @param $auto_id
     * @return mixed
     * 根据企业ID查询信息
     */
    public function GetItem($auto_id){
        return $this->mItem->GetItem($auto_id);

    }
    /**
     *
     * 项目列表
     */
    public function ItemList(){
        return $this->mItem->ItemList();

    }

    /**
     * 首页项目列表
     */
    public function IndexItem(){
        // 取得新手标
        $where = 'NEWBIE';
        $status =array('COLLECTING','REPAYING','FINISH');
        $status_new = array('COLLECTING');
        $newbieItem = $this->mItem->NewbieItemOne($where,$status_new);

        // 取得普通标
        $where = 'GENERAL';
        $items = $this->mItem->NewbieItem($where,$status);

        $tpldata = [
            // 新手标的数据
            'newbieItem' => $newbieItem,
            // 普通标的数据
            'items'      => $items,
        ];
        return $tpldata;
    }




    /**
     *
     * 项目添加
     */
    public function AddItem($paramObj){
        $paramObj->business_ticket_img = !empty($paramObj->business_ticket_img) ?
            $paramObj->business_ticket_img =  QiniuUpload::instance()->upload_files($paramObj->business_ticket_img):'';
        $paramObj->prove_file = !empty($paramObj->prove_file) ?
            $paramObj->prove_file =  QiniuUpload::instance()->upload_files($paramObj->prove_file):'';
        $paramObj->prove_file1 = !empty($paramObj->prove_file1) ?
            $paramObj->prove_file1 =  QiniuUpload::instance()->upload_files($paramObj->prove_file1):'';
        $paramObj->prove_file2 = !empty($paramObj->prove_file2) ?
            $paramObj->prove_file2 =  QiniuUpload::instance()->upload_files($paramObj->prove_file2):'';
        $paramObj->prove_file3 = !empty($paramObj->prove_file3) ?
            $paramObj->prove_file3 =  QiniuUpload::instance()->upload_files($paramObj->prove_file3):'';
        $paramObj->prove_file4 = !empty($paramObj->prove_file4) ?
            $paramObj->prove_file4 =  QiniuUpload::instance()->upload_files($paramObj->prove_file4):'';
        $paramObj->prove_file5 = !empty($paramObj->prove_file5) ?
            $paramObj->prove_file5 =  QiniuUpload::instance()->upload_files($paramObj->prove_file5):'';

        $zData = [
            'business_ticket_img'        => $paramObj->business_ticket_img,
            'user_where'      => $paramObj->user_where,
            'guarantee_way'              => $paramObj->guarantee_way,
            'repay_where'               => $paramObj->repay_where,
            'conclusion'       => $paramObj->conclusion,
            'prove_file'           => $paramObj->prove_file,
            'prove_file1'           => $paramObj->prove_file1,
            'prove_file2'           => $paramObj->prove_file2,
            'prove_file3'           => $paramObj->prove_file3,
            'prove_file4'           => $paramObj->prove_file4,
            'prove_file5'           => $paramObj->prove_file5,
            'risk_assessment'   => $paramObj->risk_assessment,
            'add_time'             => time(),
        ];
        $r_id =  $this->mRiskControl->insert($zData);
        $aData = [
            'debtor_id'        => $paramObj->debtor_id,
            'title'      => $paramObj->title,
            'type'              => $paramObj->type,
            'number'               => $paramObj->number,
            'amount'       => $paramObj->amount,
            'rate_debtor'           => $paramObj->rate_debtor,
            'rate_loaner'              => $paramObj->rate_loaner,
            'deadline'               => $paramObj->deadline,
            'refund_type'                => $paramObj->refund_type,
            'is_award'                   => $paramObj->is_award,
            'is_takeup'                   => $paramObj->is_takeup,
            'is_subsidy'                   => $paramObj->is_subsidy,
            'is_pact'                   => $paramObj->is_pact,
            'start_interest'                => $paramObj->start_interest,
            'begin_amount'                => $paramObj->begin_amount,
            'begin_amount_sx'                => $paramObj->begin_amount_sx,
            'safety_instructions'                => $paramObj->safety_instructions,
            'cost_description'                => $paramObj->cost_description,
            'status'                => ItemEnum::ITEM_STATUS_DSH,
            'add_time'             => time(),
            'risk_control'        =>$r_id,
        ];
        return $this->mItem->insert($aData);
    }
    /**
     *
     * 项目审核
     */
    public function UpItem($paramObj){
        $paramObj->business_ticket_img = !empty($paramObj->business_ticket_img) ?
            $paramObj->business_ticket_img =  QiniuUpload::instance()->upload_files($paramObj->business_ticket_img):$paramObj->business_ticket_img_old;
        $paramObj->prove_file = !empty($paramObj->prove_file) ?
            $paramObj->prove_file =  QiniuUpload::instance()->upload_files($paramObj->prove_file):$paramObj->prove_file_old;
        $paramObj->prove_file1= !empty($paramObj->prove_file1) ?
            $paramObj->prove_file1 =  QiniuUpload::instance()->upload_files($paramObj->prove_file1):$paramObj->prove_file1_old;
        $paramObj->prove_file2 = !empty($paramObj->prove_file2) ?
            $paramObj->prove_file2 =  QiniuUpload::instance()->upload_files($paramObj->prove_file2):$paramObj->prove_file2_old;
        $paramObj->prove_file3 = !empty($paramObj->prove_file3) ?
            $paramObj->prove_file3 =  QiniuUpload::instance()->upload_files($paramObj->prove_file3):$paramObj->prove_file3_old;
        $paramObj->prove_file4 = !empty($paramObj->prove_file4) ?
            $paramObj->prove_file4 =  QiniuUpload::instance()->upload_files($paramObj->prove_file4):$paramObj->prove_file4_old;
        $paramObj->prove_file5 = !empty($paramObj->prove_file5) ?
            $paramObj->prove_file5 =  QiniuUpload::instance()->upload_files($paramObj->prove_file5):$paramObj->prove_file5_old;

        $zData = [
            'business_ticket_img'        => $paramObj->business_ticket_img,
            'user_where'      => $paramObj->user_where,
            'guarantee_way'              => $paramObj->guarantee_way,
            'repay_where'               => $paramObj->repay_where,
            'conclusion'       => $paramObj->conclusion,
            'prove_file'           => $paramObj->prove_file,
            'prove_file1'           => $paramObj->prove_file1,
            'prove_file2'           => $paramObj->prove_file2,
            'prove_file3'           => $paramObj->prove_file3,
            'prove_file4'           => $paramObj->prove_file4,
            'prove_file5'           => $paramObj->prove_file5,
            'risk_assessment'   => $paramObj->risk_assessment,
        ];
        $this->mRiskControl->update($zData,['auto_id'=>$paramObj->risk_id]);
        $aData = [
            'debtor_id'        => $paramObj->debtor_id,
            'title'      => $paramObj->title,
            'type'              => $paramObj->type,
            'number'               => $paramObj->number,
            'amount'       => $paramObj->amount,
            'rate_debtor'           => $paramObj->rate_debtor,
            'rate_loaner'              => $paramObj->rate_loaner,
            'deadline'               => $paramObj->deadline,
            'refund_type'                => $paramObj->refund_type,
            'is_subsidy'                   => $paramObj->is_subsidy,
            'is_award'                   => $paramObj->is_award,
            'is_takeup'                   => $paramObj->is_takeup,
            'is_pact'                   => $paramObj->is_pact,
            'start_interest'                => $paramObj->start_interest,
            'begin_amount'                => $paramObj->begin_amount,
            'begin_amount_sx'                => $paramObj->begin_amount_sx,
            'safety_instructions'                => $paramObj->safety_instructions,
            'cost_description'                => $paramObj->cost_description,
            'status'                => $paramObj->status,

        ];
        return $this->mItem->update($aData,['auto_id'=>$paramObj->auto_id]);
    }
    /**
     *项目发布
     */
    public function ItemFb($paramObj){
        $aData = [
        'status'                => ItemEnum::ITEM_STATUS_MJZ,
            ];
        return $this->mItem->update($aData,['auto_id'=>$paramObj->auto_id]);
    }
    /**
     * 根据auto_id查询
     */
    public function GetCompanyId($auto_id){
        return $this->mEnterprise->GetCompanyId($auto_id);
    }
    /**
     * 查询项目表
     *
     */
    public function GetItemId($auto_id){
        return $this->mItem->GetItemId($auto_id);
    }


    /**
     * 项目还款详情
     * @param $auto_id
     * @return mixed
     * @author lzhp
     */
    public function RefundItemPlan($auto_id)
    {
        return $this->mItem->RefundItemPlan($auto_id);
    }


    /**
     * @param $risk_control
     * 查询风控表
     */
    public function GetRiskId($risk_control){
        return $this->mRiskControl->GetRiskId($risk_control);
    }


    /**
     * 显示投标明细
     */
    public function ItemDetail($itemid)
    {

            $item_id = $itemid;
            $status  = 'SUCCESS';

        // 项目投资的总条数
        $count = $this->mInvest->GetInvestCount($item_id,$status);
        $data = $this->mInvest->GetInvestInfo($item_id,$status);

        // 投标总金额
        $investTotalAmount = 0;
        // 使用红包总金额
        $awardTotalAmount  = 0;
        if(empty($data))
        {
            $tpldata = [
                'topInfo'  => '该项目已经投资完毕',
                'jieshi'   => 1,
            ];
        }
        else
        {
            // 循环遍历提取想要的值和赋值
            foreach ($data as $key => &$value)
            {
                // 获取字段中包含award_id的字段的id值
                if ($value->award_id)
                {
                    $awardID[] = $value->award_id;
                }
                else
                {
                    $awardID[] = '';
                }

                // 将状态给格式化
                $value->china_status = InvestEnum::status()[$value->status];
                // 将添加时间格式化
                $value->addtime     = date('Y-m-d H:i:s', $value->add_time);
                // 依次累加值
                $investTotalAmount     += $value->amount;
                // 将amount的值格式化
                $value->amount       = number_format($value->amount/100, 2).'元';

            }
            unset($value);

            // 判断当前标是否有人使用红包
            if (empty($awardID))
            {
                // 当前标没有红包
                $awardValue = '';
            }
            else
            {
                 //当前标有红包
                $awardValue = $this->mUserAward->ItemAward($awardID);


                 //循环获取将红包的值进行累加
                foreach ($awardValue as $k => $v)
                {
                    $awardTotalAmount += $v->amount;
                }

                 //循环赋值数据
                foreach ($awardValue as $key => &$value)
                {
                    // 红包激活时间
                    if ($value->active_time!=0)
                    {
                        $value->active_time = date('Y-m-d H:i:s', $value->active_time);
                    }
                    // 红包到期时间
                    $value->due_time        = date('Y-m-d H:i:s', $value->due_time);
                    // 公报添加时间
                    $value->add_time        = date('Y-m-d H:i:s', $value->add_time);
                    // 红包状态
                    $value->status          = UserEnum::status()[$value->status];
                    // 红包获取路径
                    $value->road            = UserEnum::road()[$value->road];
                    // 红包总金额
                    $value->amount          = number_format($value->amount/100, 2).'元';
                    // 投资激活限制金额
                    $value->restrict_amount = number_format($value->restrict_amount/100, 2).'元';

                }

                unset($value);
            }

            // 将数据分配到数组中
            $investInfo = [
                'auto_id'           => '投资ID',
                'trade_num'         => '投资订单号',
                'user_id'           => '用户ID',
                'item_id'           => '项目ID',
                'rate'              => '投资利率',
                'amount'            => '投资金额',
                'award_id'          => '使用红包',
                'ticket_id'         => '加息券',
                'china_status'      => '状态',
                'addtime'           => '时间',
            ];

            //红包的总数据
            $baoInfo = [
                'investTotalAmount' => '投标总金额',
                'awardTotalAmount'  => '红包总金额'
            ];

            //红包的数据说明
            $awardInfo = [
                'auto_id'         => '红包的ID',
                'user_id'         => '用户的ID',
                'trade_num'       => '投资订单号',
                'amount'          => '红包金额',
                'restrict_amount' => '投资激活限制金额',
                'road'            => '红包获取路径',
                'status'          => '红包状态',
                'active_time'     => '激活时间',
                'due_time'        => '红包到期时间',
                'add_time'        => '添加时间',
            ];

            //数据分配到数组中
            $tpldata = [
                //标题名
                'topInfo'           => '投标总数:'.$count,
                //字段名
                'investInfo'        => $investInfo,
                //总数据
                'data'              => $data,
                //红包的详情
                'awardValue'        => $awardValue,
                //红包的列表名解释
                'awardInfo'         => $awardInfo,
                //投标总金额
                'investTotalAmount' => number_format($investTotalAmount/100, 2).'元',
                //红包总金额
                'awardTotalAmount'  => number_format($awardTotalAmount/100, 2).'元',
            ];
        }
        return $tpldata;
    }


    /**
     * 投资人还款计划列表
     */
    public function loaner_plan($itemid = 0)
    {
        // 查询出投资人还款计划
        $plan  = table('item\\RefundPlan')->where('item_id', $itemid)->query();
        // 总数统计
        $count = table('item\\RefundPlan')->where('item_id', $itemid)->count();
        // 数据说明
        $fieldInfo = [
            'auto_id'        => 'ID',
            'user_id'        => '用户ID',
            'item_id'        => '项目ID',
            'amount_capital' => '回款本金',
            'amount_accrual' => '回款利息',
            'stage'          => '回款阶段',
            'start_time'     => '起始时间',
            'refund_time'    => '应还款时间',
            'status'         => '是否已回款'
        ];

        // 回款本金总数
        $amount_capital = '';
        // 回款利息总数
        $amount_accrual = '';
        // 数据转换
        foreach ($plan as $key => &$value)
        {
            // 回款本金总数累加
            $amount_capital          += $value['amount_capital']/100;
            // 回款利息总数累加
            $amount_accrual          += $value['amount_accrual']/100;
            // 回款本金
            $value['amount_capital'] = number_format($value['amount_capital']/100, 2).'元';
            // 回款利息
            $value['amount_accrual'] = number_format($value['amount_accrual']/100, 2).'元';
            // 起始时间
            $value['start_time']     = date('Y-m-d H:i:s', $value['start_time']);
            // 应还款时间
            $value['refund_time']    = date('Y-m-d H:i:s', $value['refund_time']);

        }
        unset($value);

        // 拼装到数组中
        $tpldata = [
            'topInfo'        => '投资人还款计划列表, 总数:&emsp;'.$count,
            // 字段信息
            'fieldInfo'      => $fieldInfo,
            // 数据信息
            'data'           => $plan,
            // 回款本金总数
            'amount_capital' => number_format($amount_capital, 2).'元',
            // 回款利息总数
            'amount_accrual' => number_format($amount_accrual, 2).'元'
        ];
        // 发送到视图
        $this->assign($tpldata);
        $this->display();
    }


    /**
     * 标的还款计划
     */
    public function ItemPlan($itemid)
    {
        // 查询出投资人还款计划
        $plan  = $this->mRefundItemPlan->ItemPlan($itemid);
        // 统计出数量
        $count  = $this->mRefundItemPlan->ItemPlanCount($itemid);
        // 字段说明
        $fieldInfo = [
            'auto_id'                => 'ID',
            'item_id'                => '项目ID',
            'trade_num'              => '还款预处理流水号',
            'amount_capital'         => '应还本金',
            'amount_loaner_accrual'  => '应还投资人还款利息',
            'amount_reality_accrual' => '实际应还利息',
            'stage'                  => '还款阶段',
            'start_time'             => '起始时间',
            'refund_time'            => '还款时间',
            'status'                 => '是否已还款'
        ];

        // 应还本金总数
        $amount_capital         = '';
        // 应还投资人还款利息总数
        $amount_loaner_accrual  = '';
        // 实际应还利息总数
        $amount_reality_accrual = '';
        // 数据转换
        foreach ($plan as $key => &$value)
        {
            // 应还本金总数
            $amount_capital                  += $value->amount_capital/100;
            // 应还投资人还款利息总数
            $amount_loaner_accrual           += $value->amount_loaner_accrual/100;
            // 实际应还利息总数
            $amount_reality_accrual          += $value->amount_reality_accrual/100;
            // 应还本金
            $value->amount_capital         = number_format($value->amount_capital/100, 2).'元';
            // 应还投资人还款利息
            $value->amount_loaner_accrual  = number_format($value->amount_loaner_accrual/100, 2).'元';
            // 实际应还利息
            $value->amount_reality_accrual = number_format($value->amount_reality_accrual/100, 2).'元';
            // 开始时间
            $value->start_time             = date('Y-m-d H:i:s', $value->start_time);
            // 还款时间
            $value->refund_time            = date('Y-m-d H:i:s', $value->refund_time);
            // 是否已还款
            $value->status                 = RefundEnum::status()[$value->status];

        }
        unset($value);

        // 拼装数据
        $tpldata = [
            // 头部信息
            'topInfo'                => '标的还款计划, 总数:&emsp;'.$count,
            // 字段信息
            'fieldInfo'              => $fieldInfo,
            // 数据信息
            'data'                   => $plan,
            // 应还本金总数
            'amount_capital'         => number_format($amount_capital, 2).'元',
            // 应还投资人还款利息总数
            'amount_loaner_accrual'  => number_format($amount_loaner_accrual, 2).'元',
            // 实际应还利息总数
            'amount_reality_accrual' => number_format($amount_reality_accrual, 2).'元'
        ];

        // 发送到视图页面
        return $tpldata;
    }
    public function InvestList(){
        return $this->mInvest->InvestList();
    }



/********************************************************PC端*************************************************/

    /**
     *lyc
     *我要理财
     */
    public function GetFinancing(){
        $data = $financing = $this->mItem->GetFinancing();
        // 项目募集中状态排前
        $collectItem = [];
        $StatusItem = [];
        foreach ($data as $key => $value)
        {
            if (!empty($value->status))
            {
                $StatusItem[] = $value;
            }
        }
        foreach ($data as $key => $value)
        {
            if ($value->status == 'COLLECTING'&&$value->type=='NEWBIE')
            {
                $collectItem[] = $value;
                unset($StatusItem[$key]);
            }
        }
        foreach ($data as $key => $value)
        {
            if ($value->status == 'COLLECTING'&&!$value->type=='NEWBIE')
            {
                $collectItem[] = $value;
                unset($StatusItem[$key]);
            }
        }
        $dataitem = array_merge($collectItem,$StatusItem);

        foreach ($dataitem as $key => $value)
        {
            $data[$key] = $dataitem[$key];
        }
        return $data;
    }
    /**
     *lyc
     *理财详情
     */
    public function GetFinancingDetails($paramObj){
        return $financing = $this->mItem->GetFinancingDetails($paramObj);
    }
    /**
     *
     * 投资记录
     */
    public function GetInvest($paramObj){
        return $invest= $this->mInvest->GetInvest($paramObj);
    }
    /**
     * 查询投资记录人手机号
     */
    public function GetUserIphone($user_id){
        return $invest= $this->mUser->GetUserIphone($user_id);
    }
    /**
     * @param $auto_id
     * @return mixed
     * 查询是否存在该项目，状态是否能投
     */
    public function IsItem($auto_id){
        return $this->mItem->IsItem($auto_id);

    }
    /**
     * 是否存在红包
     */
    public function IsAward($user_id){
        return $this->mUserAward->IsAward($user_id);

    }
    /**
     * 是否存在加息券
     */
    public function IsTicket($user_id){
        return $this->mUserTicket->IsTicket($user_id);
    }

    /**
     * 体验标列表
     * @return mixed
     */
    public function ItemNewList()
    {
        return $this->mExperItem->ItemNewList();
    }

    /**
     * 首页体验标
     * @return mixed
     */
    public function TiYanItemList()
    {
        return $this->mExperItem->TiYanItemList();
    }

    /**
     * 体验标添加
     * @param $paramObj
     * @return int
     */
    public function ItemNewAdd($paramObj)
    {

        #添加
        $aData = [
            'exper_name'             =>     $paramObj->exper_name,
            'exper_amount'           =>     $paramObj->exper_amount,
            'expect_rate'            =>     $paramObj->expect_rate,
            'deadline'               =>     $paramObj->deadline,
//            'active_time'            =>     $paramObj->active_time,
//            'due_time'               =>     $paramObj->due_time,
            'is_valid'               =>     $paramObj->is_valid,
            'add_time'               =>     time(),
//            'status'                 =>     'A',
        ];

        return $this->mExperItem->insert($aData);
    }

    /**
     * 体验标修改
     * @param $paramObj
     * @return bool
     */
    public function ItemNewUP($paramObj)
    {
        #修改
        $aData = [
            'exper_name'             =>     $paramObj->exper_name,
            'exper_amount'           =>     $paramObj->exper_amount,
            'expect_rate'            =>     $paramObj->expect_rate,
            'deadline'               =>     $paramObj->deadline,
//            'active_time'            =>     $paramObj->active_time,
//            'due_time'               =>     $paramObj->due_time,
            'is_valid'               =>     $paramObj->is_valid,
            'add_time'               =>     time(),
//            'status'                 =>     $paramObj->status,
        ];

        return $this->mExperItem->update($aData,['auto_id'=>$paramObj->auto_id]);

    }

    /**
     * 获取体验标信息
     * @param $auto_id
     * @return mixed
     */
    public function GetItemNewInfo($auto_id)
    {
        return $this->mExperItem->GetItemNewInfo($auto_id);
    }

    /**
     * 是否存在加息券
     */
    public function NewbieItemOne(){
        $where = 'NEWBIE';
        $status_new = array('COLLECTING');
        return $this->mItem->NewbieItemOne($where,$status_new);
    }

    /**
     * 是否存在加息券
     */
    public function ApiNewbieItem(){
        $where = 'NEWBIE';
        $status_new = array('COLLECTING');
        return $this->mItem->ApiNewbieItem($where,$status_new);
    }


    /**
     * 是否启用此体验标
     * @param $auto_id
     * @param $is_valid
     * @return mixed
     */
    public function IsValid($auto_id,$is_valid)
    {
        if($is_valid==1){
            $aData = [
                'is_valid' => '0',
            ];
            $this->mExperItem->update($aData,['auto_id'=>$auto_id]);
            return $this->mExperItem->ItemNewList();
        }else{
            $aData = [
                'is_valid' => '1',
            ];
            $this->mExperItem->update($aData,['auto_id'=>$auto_id]);
            return $this->mExperItem->ItemNewList();
        }
    }

}